package YYX.Caculator01;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * @ClassName: Mycalculator
 * @Auther: Drawin
 * @Date: 2021/12/24 15:33
 * @Description:计算器小程序
 * 要求:使用图形用户界面
 * 能在键盘或鼠标上读入数据,并完成加。减。乘。除计算。
 * 在屏幕上显示一个主菜单。提示用户输入 相应的数字键,分别执行加。减。乘。除计算功能和结束程序的功能。
 */

public class Mycalculator extends JFrame {

    private String[]name={"1","2","3","+","4","5","6","-","7","8","9","*","0",".","=","/"};
    JButton[] jButton= new JButton[name.length];
    private double num=0;
    private String command="=";//运算符
    private boolean start=true;//判断标识符
    private JTextField jTextField = new JTextField(20);//一个文本框,放在外面，不然后面的方法调用不了

    public Mycalculator(){
        Container container = this.getContentPane();

        JPanel jPane1 = new JPanel();//输入框面板
        JPanel jPane2 = new JPanel(new GridLayout(4,4));//键盘面板


        //十七个按钮和监听
        ButtonAction ba = new ButtonAction();
        KeyAction ka = new KeyAction();
        Ce ce1 = new Ce();
        JButton ce = new JButton("CE");
        ce.addActionListener(ce1);
        for (int i = 0; i < name.length; i++) {
            jButton[i]=new JButton(name[i]);
            jButton[i].addActionListener(ba);
            jButton[i].addKeyListener(ka);
            jPane2.add(jButton[i]);//按钮装到面板上
            Font font = new Font("Courier New", Font.BOLD, 22);//按钮太小了，字体调大点
            jButton[i].setFont(font);
        }

        //组装
        jPane1.add(jTextField,BorderLayout.WEST);
        jPane1.add(ce,BorderLayout.EAST);
        jPane1.setBackground(new Color(102, 204, 255));
        this.add(jPane1,BorderLayout.NORTH);
        this.add(jPane2,BorderLayout.CENTER);

        this.setVisible(true);
        this.setBounds(300,200,500,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public void calculate(double x){
        if (command.equals("+"))
            num+=x;
        else if (command.equals("-"))
            num-=x;
        else if (command.equals("*"))
            num*=x;
        else if (command.equals("/"))
            num/=x;
        else if (command.equals("="))
            num=x;
        jTextField.setText(""+num);//num是double不是String所以这样转一下
    }

   private class ButtonAction implements ActionListener {//内部类，抽一个按钮监听器出来
       @Override
       public void actionPerformed(ActionEvent e) {
           String input=e.getActionCommand();
           if (input.equals("+")||input.equals("-")||input.equals("*")||input.equals("/")||
                   input.equals("=")){
               if(start){
                   if (input.equals("-")){//负数
                       jTextField.setText(input);
                       start=false;
                   }else
                       command=input;
               }else{
                   calculate(Double.parseDouble(jTextField.getText()));
                   command=input;
                   start=true;

               }
           }else{
               if(start){
                   jTextField.setText("");
                   start=false;
               }
               jTextField.setText(jTextField.getText()+input);
           }

       }
   }

    private  class KeyAction extends KeyAdapter{//继承适配器,不用实现接口的所有方法
        public void keyTyped(KeyEvent e) {
            char key = e.getKeyChar();
            if(key=='0'||key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9') {
                if(start) {
                    jTextField.setText("");
                    start=false;
                }
                jTextField.setText(jTextField.getText()+key);
            }
            else if(key=='+'||key=='-'||key=='/'||key=='*') {
                if(start) {
                    if(key=='-') {
                        jTextField.setText(String.valueOf(key));
                        start=false;
                    }else
                        command=String.valueOf(key);

                }else {
                    calculate(Double.parseDouble(jTextField.getText()));
                   command=String.valueOf(key);
                    start=true;
                }
            }
        }

    }
private class Ce implements ActionListener{//清空键监听

    @Override
    public void actionPerformed(ActionEvent e) {
        jTextField.setText("");
    }
}


    public static void main(String[] args) {//启动器
     new Mycalculator();
    }
}
